//
//  SelectView.m
//  yameishe3
//
//  Created by pro on 2017/3/8.
//  Copyright © 2017年 yameishe. All rights reserved.
//

#import "SelectView.h"
#import "MediaSelectCell.h"

@interface SelectView ()
{
    __weak IBOutlet UILabel *_titleLab;
    __weak IBOutlet UITableView *_tableView;
    __weak IBOutlet UIButton *_removeBtn;
}
@end
@implementation SelectView
-(void)awakeFromNib
{
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50)];
    [_tableView registerNib:[UINib nibWithNibName:@"MediaSelectCell"bundle:nil] forCellReuseIdentifier:@"MediaSelectCell"];
}
-(void)setSelectArray:(NSMutableArray *)selectArray
{
    _selectArray = selectArray;
    [_tableView reloadData];
}

#pragma mark -- UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _selectArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MediaSelectCell* cell = [tableView dequeueReusableCellWithIdentifier:@"MediaSelectCell"];
    YMSMediaSource *source = _selectArray[indexPath.row];
    cell.source = source;
    [cell setCallbackDidDelete:^(YMSMediaSource *source) {
        [_selectArray removeObject:source];
        if (_callbackDidChangeArray) {
            _callbackDidChangeArray(_selectArray);
        }
        [_tableView deleteRow:indexPath.row inSection:indexPath.section withRowAnimation:UITableViewRowAnimationNone];
    }];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

-(IBAction)onTouchRemoveAll:(id)sender
{
    [_selectArray removeAllObjects];
    [_tableView reloadData];
    if (_callbackDidChangeArray) {
        _callbackDidChangeArray(_selectArray);
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
